package com.muyi.controller;

import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class CaptchaController {
    /**
     * 生成验证码
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getCaptcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 使用这一行代码就可以生成验证码了
        CaptchaUtil.out(request, response);
    }

    @PostMapping("/verifyCaptcha")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取验证码参数
        String captcha = request.getParameter("captcha");
        // 校验验证码是否正确
        if (CaptchaUtil.ver(captcha, request)) {
            response.getOutputStream().print("captcha correct!");
        } else {
            response.getOutputStream().print("captcha error!");
        }
    }


}
